local skynet = require "skynet"
local filelog = require "filelog"
local base = require "base"
local configdao = require "configdao"
local filename = "gatenodecmd.lua"
local service = nil
local GatenodeCMD = {
	service = nil
}

function GatenodeCMD.process(session, source, event, ...)
	if not service then
		service = GatenodeCMD.service
	end
	local f = GatenodeCMD[event] 
	if f == nil then
		filelog.sys_error(filename.." line17 GatenodeCMD.process invalid event:"..event)
		return nil
	end
	skynet.retpack(f(...))
end

function GatenodeCMD.start(conf)
	service.tcpmng.init(service, "agent", conf.agentsize, conf.agentincr, "websocketnetpack")

	skynet.fork(function()
		skynet.sleep(100)
		skynet.call(service.gate_service, "lua", "open" , conf)
	end)
	return true
end

function GatenodeCMD.close(...)
	service:exit_service()	
end

-- 这里和 loginnodecmd.agentexit 有不同，要带上rid
-- LTM 需要rid 才能去找到对应的agent
function GatenodeCMD.agentexit(fd, rid)
	service.tcpmng.agentexit(fd, rid)
end

--处理玩家心跳超时关闭socket
function GatenodeCMD.heart_timeout(fd)
	service.tcpmng.heart_timeout(fd)
end

-- 其他node调用 router的notify(),然后调用到这里
function GatenodeCMD.notify( ... )
	return service:notify( ... )
end

-- 给一组人发广播同样消息
function GatenodeCMD.rids_broadcast_func( ... )
	return service:rids_broadcast_func( ... )
end

-- 获取所有玩家
function GatenodeCMD.get_all_agents()
	return service:get_all_agents()
end

-- 得到某个agent的service，主要是为了使用里面的 long_tcp_mng
function GatenodeCMD.get_agent_by_rid( ...)
	return service:get_agent_by_rid( ... )
end

-- 修改所有agent的里的ab服设置
function GatenodeCMD.set_gamenode_config_ab( ...)
	return service:set_gamenode_config_ab( ... )
end

-- 发送http请求
function GatenodeCMD.send_http_request( ...)
	return service:send_http_request( ... )
end

--通知平台服发送验证码
function GatenodeCMD.notice_platform_server( ...)
	return service:notice_platform_server( ... )
end

function GatenodeCMD.send_bindagency_request(...)
	return service:send_bindagency_request( ... )
end

function GatenodeCMD.reload(...)
	skynet.retpack(1)
	configdao.reload()
	skynet.sleep(200)
end

return GatenodeCMD